/*
 * Copyright 2012 Clement Sannicolo <clement.sannicolo@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Ext.define('Nutrient.view.food.List',
{
	extend: 'Ext.panel.Panel',
	alias: 'widget.nutrient.food.list',

	requires: [
	    'Ext.view.View'
	],

	autoScroll: true,

	items: [{
		xtype: 'dataview',
		tpl: [
		    '<tpl for=".">',
		    	'<div class="item no-select {[xindex % 2 === 0 ? "even" : "odd"]}">',
		    		'<div class="name" title="{name}">{name}</div>',
		    		'<div class="category">Categorie: {category}</div>',
		    		'<div class="source">Source: {source}</div>',
		   		'</div>',
			'</tpl>'
		],
		itemSelector: 'div.item',
		emptyText: '<div class="empty">No items available</div>',
		store: 'Nutrient.store.Search',
		listeners:
		{
			itemclick: function(dataview, record, item, index, event, options)
			{
				App.dispatch({
					controller: 'Nutrient.controller.Food',
					action: 'show',
					id: record.getId()
				});
			},
			itemcontextmenu: function(view, record, item, index, event, options)
			{
				event.preventDefault();
				Ext.create('Ext.menu.Menu', {
					margin: '0 0 10 0',
					items: [{
						itemId: 'add',
						iconCls: 'icon-add',
						text: 'New'
					}, {
						itemId: 'edit',
						iconCls: 'icon-edit',
						text: 'Edit'
					}, {
						itemId: 'remove',
						iconCls: 'icon-delete',
						text: 'Delete'
					}],
					listeners: {
						click: function(menu, item, event, options) {
							App.dispatch({
								controller: 'Nutrient.controller.Food',
								action: item.itemId,
								id: record.getId()
							});
						}
					}
				}).showAt(event.getXY());
			}
		}
	}]
});